package com.ztz.mynewdemo.utils;

import com.google.gson.Gson;

import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 网络访问工具类
 */
public class RequestUtils {


    private static final int SERVIDEID = 1;
//    private static final int SERVIDEID = 2;


    private static final String CONTENT_TYPE_JSON = "application/json";

    //头值
    private static final String HEADER_X_LC_ID = "X-LC-Id";
    private static final String HEADER_X_LC_KEY = "X-LC-Key";
    private static final String HEADER_CONTENT_TYPE = "Content-Type";

    //1
    private static final String X_LC_ID_VALUE_1 = "Fkaab1ktpqbWBJyufu0Regpd-gzGzoHsz";
    private static final String X_LC_KEY_VALUE_1 = "LuF6Ry6MARcH4ByMPg9M8vOF";

    //2
    private static final String X_LC_ID_VALUE_2 = "8XDYKB2iWCl9sHI40mMdXnps-gzGzoHsz";
    private static final String X_LC_KEY_VALUE_2 = "mwqO11XKQXzxp5P0SA9NVIqw";

    private static MediaType JSONTYPE = MediaType.parse("application/json; charset=utf-8");



    public static Request.Builder getBuilder() {
        Request.Builder builder = new Request.Builder();
        return builder;
    }

    private static Request.Builder getRequest1(HttpUrl url) {
        Request.Builder request = getBuilder().url(url)//
                .addHeader(HEADER_X_LC_ID, X_LC_ID_VALUE_1)//
                .addHeader(HEADER_X_LC_KEY, X_LC_KEY_VALUE_1)//
                .addHeader(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON);
        return request;
    }

    public static Request.Builder getRequest2(HttpUrl url) {
        Request.Builder builder = getBuilder().url(url)
                .addHeader(HEADER_X_LC_ID, X_LC_ID_VALUE_2)
                .addHeader(HEADER_X_LC_KEY, X_LC_KEY_VALUE_2)
                .addHeader(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON);
        return builder;
    }


    public final static HttpUrl getParseUrl(String url) {
        if (url != null && !"".equals(url)) {
            return HttpUrl.parse(url).newBuilder().build();
        }
        throw new NullPointerException("url 是空的");
    }


    /**
     * @param url 请求地址
     * @return Request
     */
    public static Request requestGET(String url) {
        HttpUrl httpUrl = getParseUrl(url);
        Request request = null;
        switch (SERVIDEID) {
            case 1:
                request = requestGET(httpUrl);
                break;
            case 2:
                request = requestGET(httpUrl);
                break;
        }
        return request;
    }


    /**
     * DELETE 请求
     *
     * @param url
     * @return Request
     */
    public final static Request requestDELETE(String url) {
        Request request = null;
        switch (SERVIDEID) {
            case 1:
                request = getRequest1(getParseUrl(url)).delete().build();
                break;
            case 2:
                request = getRequest2(getParseUrl(url)).delete().build();
                break;
        }
        return request;
    }


    /**
     * GET 请求
     *
     * @param url
     * @return
     */
    public static Request requestGET(HttpUrl url) {
        Request request = null;
        switch (SERVIDEID) {
            case 1:
                request = getRequest1(url).get().build();
                break;
            case 2:
                request = getRequest2(url).get().build();
                break;
        }
        return request;
    }


    /**
     * POST 请求
     *
     * @param url 请求地址
     * @return Request
     */
    public static Request requestPOST(Object par, String url) {
        HttpUrl httpUrl = getParseUrl(url);
        RequestBody body = RequestBody.create(JSONTYPE, new Gson().toJson(par));
        Request request = null;
        switch (SERVIDEID) {
            case 1:
                request = getRequest1(httpUrl).post(body).build();
                break;
            case 2:
                request = getRequest2(httpUrl).post(body).build();
                break;
        }
        return request;
    }

    /**
     * POST 空值请求
     *
     * @param url 请求地址
     * @return Request
     */
    public static Request requestPOSTNULL(String url) {
        HttpUrl httpUrl = getParseUrl(url);
        Request request = null;
        switch (SERVIDEID) {
            case 1:
                request = getRequest1(httpUrl).post(okhttp3.internal.Util.EMPTY_REQUEST).build();
                break;
            case 2:
                request = getRequest2(httpUrl).post(okhttp3.internal.Util.EMPTY_REQUEST).build();
                break;
        }
        return request;
    }


}
